/* 
 * File:   BlockingQueueObject.cpp
 * Author: lucia
 * 
 * Created on 2 de noviembre de 2014, 16:03
 */

#include "BlockingQueueObject.h"

BlockingQueueObject::BlockingQueueObject() {
}

void BlockingQueueObject::push(struct ObjectInit data) {

    std::lock_guard<std::mutex> lock(this->mtx);
    this->queue.push(data);
    
    this->cv.notify_all();
}

struct ObjectInit BlockingQueueObject::waitAndPop() {
    std::unique_lock<std::mutex> uni_lock(this->mtx);

    cv.wait(uni_lock,[this]{return !this->queue.empty();});

    struct ObjectInit data = this->queue.front();
    this->queue.pop();

    return data;
}

bool BlockingQueueObject::tryPop(struct ObjectInit &data) {
    std::unique_lock<std::mutex> uni_lock(this->mtx);
    if (this->queue.empty()) {
        return false;
    }

    data = this->queue.front();
    this->queue.pop();
    return true;
}

bool BlockingQueueObject::empty() {
    std::lock_guard<std::mutex> lock(this->mtx);
    return this->queue.empty();
}

int BlockingQueueObject::size(){
    return this->queue.size();
}

BlockingQueueObject::~BlockingQueueObject() {
}

